package cn.edu.imnu.service.impl;

import cn.edu.imnu.service.WordInfoService;
import common.model.DataBootstrapTable;
import common.model.ResponseModelBootstrapTable;
import common.util.Page;
import cn.edu.imnu.mapper.WordInfoMapper;
import cn.edu.imnu.model.WordInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @author ranhongmin--2018/3/3
 * Description:
 */
@Service
public class WordInfoServiceImpl implements WordInfoService {

	@Resource
	private WordInfoMapper infoMapper;

	/**
	 * 祝查询
	 *
	 * @param dataBootstrapTable 表格传值类
	 * @return 表格数据
	 */
	@Override
	public ResponseModelBootstrapTable main(DataBootstrapTable dataBootstrapTable) {
		ResponseModelBootstrapTable modelBootstrapTable = new ResponseModelBootstrapTable();

		Integer offset = dataBootstrapTable.getOffset();
		Integer pageSize = dataBootstrapTable.getLimit();
		Integer currentPage = 1;
		String search = dataBootstrapTable.getSearch();

		if (offset == 0) {
			currentPage = 1;
		} else {
			currentPage = (pageSize + offset) / pageSize;
		}

		Page page = new Page(currentPage, pageSize);

		Example example = new Example(WordInfo.class);
		Example.Criteria cri = example.createCriteria();
		//没删除的
		cri.andEqualTo("isDel", "1");
		//父类信息
		// cri.andEqualTo("upperInfoId", "0");

		List<?> wordInfo;
		//查询
		if (search != null && !"".equals(search)) {
			cri.andLike("infoTitle", "%" + search + "%");
		}
		//排序
		if (dataBootstrapTable.getSort() != null && !"".equals(dataBootstrapTable.getSort())) {
			Example.OrderBy orderBy = example.orderBy(dataBootstrapTable.getSort());
			if ("desc".equals(dataBootstrapTable.getOrder())) {
				orderBy.desc();
			} else {
				orderBy.asc();
			}
		}

		wordInfo = infoMapper.selectByExampleAndRowBounds(example,
				new RowBounds(page.getStartLine(), pageSize));
		page.setTotalLine(infoMapper.selectCountByExample(example));

		modelBootstrapTable.bing(wordInfo, page.getTotalLine(), page.getTotalPage());
		return modelBootstrapTable;
	}

	/**
	 * 增加 修改 删除 动作
	 *
	 * @param info   实体
	 * @param action 动作
	 * @return 是否成功
	 */
	@Override
	public ResponseModelBootstrapTable doAction(WordInfo info, String action) {
		ResponseModelBootstrapTable modelBootstrapTable = new ResponseModelBootstrapTable();
		try {
			switch (action) {
				case "1": {
					//	add
					info.setCreateTime(new Date());
					info.setIsDel(1);
					infoMapper.insertSelective(info);
					modelBootstrapTable.success();
				}
				break;
				case "2": {
					//	update
					info.setUpdateTime(new Date());
					info.setIsDel(1);
					infoMapper.updateByPrimaryKeySelective(info);
					modelBootstrapTable.success();
				}
				break;
				case "3": {
					//	del
					info.setIsDel(0);
					infoMapper.updateByPrimaryKeySelective(info);
					modelBootstrapTable.success();
				}
				break;
				default:
					modelBootstrapTable.error();
					break;
			}
		} catch (Exception e) {
			modelBootstrapTable.error();
		}
		return modelBootstrapTable;
	}

	/**
	 * 获取词条信息
	 *
	 * @param wordId 词条id
	 * @return 词条信息
	 */
	@Override
	public ResponseModelBootstrapTable getInfo(String wordId) {
		ResponseModelBootstrapTable modelBootstrapTable = new ResponseModelBootstrapTable();
		Example example = new Example(WordInfo.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("wordId", wordId);
		modelBootstrapTable.setList(infoMapper.selectByExample(example));
		return modelBootstrapTable;
	}


	/**
	 * 获取所有父类的信息
	 *
	 * @return
	 */
	@Override
	public ResponseModelBootstrapTable getAllUpperInfo() {
		ResponseModelBootstrapTable modelBootstrapTable = new ResponseModelBootstrapTable();

		Example example = new Example(WordInfo.class);
		Example.Criteria cri = example.createCriteria();
		//没删除的
		cri.andEqualTo("isDel", "1");
		//父类信息
		cri.andEqualTo("upperInfoId", "0");

		modelBootstrapTable.setList(infoMapper.selectByExample(example));
		return modelBootstrapTable;
	}

	/**
	 * 根据词条id获取信息
	 *
	 * @param wordId
	 * @return
	 */
	@Override
	public ResponseModelBootstrapTable getInfoListByWordId(String wordId) {
		ResponseModelBootstrapTable modelBootstrapTable = new ResponseModelBootstrapTable();

		Example example = new Example(WordInfo.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("wordId", wordId);
		modelBootstrapTable.setList(infoMapper.selectByExample(example));
		return modelBootstrapTable;
	}
}
